<?php
namespace app\api\controller\dongtaidetail;
use think\Controller;
use app\api\model\DongTai;
use app\api\model\Dianzan;
use app\api\model\ShouChang;
use app\api\model\Pinlun;
use app\api\model\User;

class DongTaiDetail extends Controller
{
	public function addDianZan()
	{
		$userId = input('userId');
		$dongtaiId = input('did');
		$temp = Dianzan::where('user_id', $userId)->where('dongtai_id', $dongtaiId)->find();
		if($temp)  // 已经点赞过直接返回
		{
		  $res = ['code'=>-1, 'msg'=> '您已经点赞过此条动态'];
		  return json($res);
		}
		$dongtai = DongTai::get($dongtaiId);
		if($dongtai) 
		{   
			// 如果动态存在在进行操作
			$dianzan = new Dianzan();
			$dianzan->dongtai_id = $dongtaiId;
			$dianzan->user_id = $userId;
			$dianzan->save();
			$dongtai->dnumber = $dongtai->dnumber + 1; // 此条动态的点赞数加1
			$dongtai->save();
			$res = ['code'=>200, 'msg'=> '点赞成功'];
			return json($res);
		} else {
			$res = ['code'=>-1, 'msg'=> '动态不存在可能被用户删除或违禁'];
			return json($res);
		}
	}
	public function cancelDianzan() 
	{
		$userId = input('userId');
		$dongtaiId = input('did');
		$dianzan = Dianzan::where('user_id', $userId)->where('dongtai_id', $dongtaiId)->find();
		if(!$dianzan)
		{
		  $res = ['code'=>-1, 'msg'=> '您还没有点赞该动态'];
		  return json($res);
		}
		$dongtai = DongTai::get($dongtaiId);
		if($dongtai) 
		{   
			// 如果动态存在在进行操作
			$dianzan->delete();
			$dongtai->dnumber = $dongtai->dnumber - 1; // 这条动态点赞数-1
			if($dongtai->dnumber < 0) {$dongtai->dnumber=0;}
			$dongtai->save();
			$res = ['code'=>200, 'msg'=> '取消点赞成功'];
			return json($res);
		} else {
			$res = ['code'=>-1, 'msg'=> '动态不存在可能被用户删除或违禁'];
			return json($res);
		}
	}
	public function addShouChang()
	{
		$userId = input('userId');
		$dongtaiId = input('did');
		$temp = ShouChang::where('user_id', $userId)->where('dongtai_id', $dongtaiId)->find();
		if($temp)  // 已经点赞过直接返回
		{
		  $res = ['code'=>-1, 'msg'=> '您已经收藏过此条动态'];
		  return json($res);
		}
		$dongtai = DongTai::get($dongtaiId);
		if($dongtai) 
		{   
			// 如果动态存在在进行操作
			$shouChang = new ShouChang();
			$shouChang->dongtai_id = $dongtaiId;
			$shouChang->user_id = $userId;
			$shouChang->save();
			$dongtai->snumber = $dongtai->snumber + 1; // 此条动态的点赞数加1
			$dongtai->save();
			$res = ['code'=>200, 'msg'=> '收藏成功'];
			return json($res);
		} else {
			$res = ['code'=>-1, 'msg'=> '动态不存在可能被用户删除或违禁'];
			return json($res);
		}
	}
	public function cancelShouChang() 
	{
		$userId = input('userId');
		$dongtaiId = input('did');
		$shouChang = ShouChang::where('user_id', $userId)->where('dongtai_id', $dongtaiId)->find();
		if(!$shouChang)
		{
		  $res = ['code'=>-1, 'msg'=> '您还没有收藏该动态'];
		  return json($res);
		}
		$dongtai = DongTai::get($dongtaiId);
		if($dongtai) 
		{   
			// 如果动态存在在进行操作
			$shouChang->delete();
			$dongtai->snumber = $dongtai->snumber - 1; // 这条动态点赞数-1
			if($dongtai->snumber < 0) {$dongtai->snumber=0;}
			$dongtai->save();
			$res = ['code'=>200, 'msg'=> '取消收藏成功'];
			return json($res);
		} else {
			$res = ['code'=>-1, 'msg'=> '动态不存在可能被用户删除或违禁'];
			return json($res);
		}
	}
	// 根据动态id获取点赞
	public function getDianZan(){
		$did = input('did'); // 获得动态id
		$data = []; // 保存点赞用户数据
		// $dianzan = Dianzan::getBydongtaiId($did);
		$dianzan = Dianzan::where('dongtai_id', $did)->select();//返回的是一个数据集，根据动态id获取点赞的用户数据
		$number = Dianzan::where('dongtai_id', $did)->count(); // 获取点赞条数
		foreach ($dianzan as $value)
		{
			$userId =  $value->user_id;  // 获得哪个用户点赞了
			$user = User::get($userId);
			$tempData = [
				'userId' => $userId,
				'imgUrl' => $user->img_url,
				'sex' => $user->sex,
				'cosNumber' => $user->cos_number,
				'date' => $value->create_time,
				'info'=> [
					'userId' => $userId,
					'name' => $user->username,
					'sex' => $user->sex,
					'time'=> $value->create_time,
					'img' => $user->img_url,
					]
				];
			array_push($data, $tempData);
		}
		 $res = [
            'msg' => '查询成功',
            'code' => 200,
            'data'=> $data,
            'dianzanNumber' => $number
        ];
        return json($res);
	}
	public function getPinlun() {
		$did = input('did'); // 获得动态id
		$page = input('page'); // 获得分页数据
		$data = []; // 保存评论用户数据
		// $dianzan = Dianzan::getBydongtaiId($did);
		$pinlun = Pinlun::where('dongtai_id', $did)->order('create_time desc')->paginate(10,true);//返回的是一个数据集，根据动态id获取点赞的用户数据
		$number = Pinlun::where('dongtai_id', $did)->count(); // 获取评论条数
		foreach ($pinlun as $value)
		{
			$userId =  $value->user_id;  // 获得哪个用户评论了
			$user = User::get($userId);
			$tempData = [
				'userId' => $userId,
				'info'=> [
					'userId' => $userId,
					'name' => $user->username,
					'sex' => $user->sex,
					'time'=> $value->create_time,
					'img' => $user->img_url,
					],
				'cosNumber' => $user->cos_number,
				'date' => $value->create_time,
				'content' => $value->content,
				];
			array_push($data, $tempData);
		}
		 $res = [
            'msg' => '查询成功',
            'code' => 200,
            'data'=> $data,
            'pinlunNumber' => $number
        ];
        return json($res);
	}
	public function addPinlun() {
		$did = input('did'); // 获得动态id
		$userId = input('uid'); // 获得用户id
		$content = input('content'); //
		$dongtai = DongTai::get($did);// 获得动态对象
		$pnumber = $dongtai->pnumber + 1;
		$dongtai->pnumber = $pnumber;
		$dongtai->save();
		$data = [
			'dongtai_id' => $did,
			'content' => $content,
			'user_id' => $userId,
			];
		$result = $dongtai->pinluns()->save($data);
		if($result)
    	{
    		$res = [
            'msg' => '评论成功',
            'code' => 200,
        	];
        	return json($res);
    	} else {
    		$res = [
            'msg' => '评论失败',
            'code' => 200,
            'data'=> '',
        	];
        	return json($res);
    	}
	}
	// 根据动态id获取动态详情
	public function getDongTaiDetail(){
		$userId = input('userId');
		$did = input('did');
	}
}